package gui;

import constant.Constant;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.*;
import server.TodoItem;

public class ConfirmConflictDialog extends JDialog implements ActionListener{

    private JButton eraseButton = null;
    private JButton updateButton = null;
    private int answer = 0;
    
    public ConfirmConflictDialog(TodoItem item, TodoItem newItem, JFrame jFrame, boolean modal) {
        super(jFrame, modal);
        setSize(600, 300);
        setTitle("Update conflict");
        JPanel panel = new JPanel(new BorderLayout());
        getContentPane().add(panel);
        
        JLabel info = new JLabel("Your item is out of date. \n"
                + "Would you like to replace item on server or update your item?");
        
        JLabel mainPanel = new JLabel();
        mainPanel.setLayout(new GridLayout(1, 2));
        DisplayPanel leftPanel = new DisplayPanel(item,0);
        DisplayPanel rightPanel = new DisplayPanel(newItem,1);
        mainPanel.add(leftPanel);
        mainPanel.add(rightPanel);
        
        eraseButton = new JButton("Erase");
        eraseButton.addActionListener(this);
        
        updateButton = new JButton("Update");
        updateButton.addActionListener(this);
        
        JPanel bottom = new JPanel();
        bottom.add(eraseButton); 
        bottom.add(updateButton);  
        
        panel.add(info,BorderLayout.NORTH);
        panel.add(mainPanel,BorderLayout.CENTER);
        panel.add(bottom, BorderLayout.SOUTH);
        setLocationRelativeTo(jFrame);
        
    }
    
    public int showDialog(){
        setVisible(true);
        return answer;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
         if(eraseButton == e.getSource()) {
            
            answer=Constant.ERASE_BUT;
            setVisible(false);
        }
        else if(updateButton == e.getSource()) {
            answer = Constant.UPDATE_BUT;
            setVisible(false);
        }
    } 
}
